package com.hontech.dapapp

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.hontech.dapapp.ui.dialog.runProg
import com.hontech.dapapp.ui.view.LabelTextView
import com.hontech.dapapp.utils.Hex
import com.hontech.dapapp.utils.Pro
import com.hontech.dapapp.utils.onClick
import com.hontech.dapapp.utils.queryHex
import com.hontech.dapapp.utils.runOnUi
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

class HexFragment(private val pro: Pro) : Fragment() {

    private val adapter = HexAdapter()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = layoutInflater.inflate(R.layout.fragment_hex, null)
        init(view)
        return view
    }

    private fun init(view: View) {
        val tvName = view.findViewById<TextView>(R.id.id_hex_tv_pro_name)
        tvName.text = pro.name

        val rv = view.findViewById<RecyclerView>(R.id.id_hex_rv)
        rv.adapter = adapter
        rv.layoutManager = LinearLayoutManager(view.context, LinearLayoutManager.VERTICAL, false)
        val swipe = view.findViewById<SwipeRefreshLayout>(R.id.id_hex_swipe)

        swipe.setOnRefreshListener {
            runOnUi {
                update()
                swipe.isRefreshing = false
            }
        }

        runOnUi { update() }
    }

    private suspend fun update() {
        runProg("获取固件", "获取固件") {
            adapter.hexs = queryHex(pro.id)
            adapter.notifyDataSetChanged()
        }
    }
}

private class HexItem(view: View) : RecyclerView.ViewHolder(view) {

    private val ltName = view.findViewById<LabelTextView>(R.id.id_item_hex_lt_name)
    private val ltVersion = view.findViewById<LabelTextView>(R.id.id_item_hex_lt_version)
    private val ltDescription = view.findViewById<LabelTextView>(R.id.id_item_hex_lt_description)
    private val ltDate = view.findViewById<LabelTextView>(R.id.id_item_hex_lt_date)
    private val ltSize = view.findViewById<LabelTextView>(R.id.id_item_hex_lt_size)
    private val ltTarget = view.findViewById<LabelTextView>(R.id.id_item_hex_lt_target)
    private val btnDownload = view.findViewById<Button>(R.id.id_item_hex_btn_download)

    fun setUp(hex: Hex) {
        ltName.set(hex.name)
        ltVersion.set(hex.version)
        ltDescription.set(hex.description)
        ltDate.set(dateFormat(hex.createTimestamp))
        ltSize.set(sizeFormat(hex.size))
        ltTarget.set(hex.target)
        btnDownload.onClick {
            runProg("固件下载", "固件下载") { dialog ->
                val dap = DapLink()
                dap.download(hex.target, hex.id) {
                    dialog.setMsg(it)
                }
            }
        }
    }
}

private class HexAdapter : RecyclerView.Adapter<HexItem>() {

    var hexs = arrayOf<Hex>()

    override fun getItemCount(): Int {
        return hexs.size
    }

    override fun onBindViewHolder(holder: HexItem, position: Int) {
        holder.setUp(hexs[position])
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HexItem {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_hex, parent, false)
        return HexItem(view)
    }
}

private fun dateFormat(timestamp: Long): String {
    val date = Date(timestamp * 1000)
    val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
    return format.format(date)
}

private fun sizeFormat(size: Long): String {
    if (size < 1024) {
        return "${size}Byte"
    } else if (size < 1024 * 1024) {
        val s = size.toFloat() / 1024.0f
        return String.format("%.2fKB", s)
    } else  {
        val s = size.toFloat() / 1024.0f / 1024f
        return String.format("%.2fMB", s)
    }
}
